﻿Namespace Catalog
    Public Class Chart
        Inherits BaseObject
        Public Property Description As String
        Public Property PublishDate As Date
        Public Property Price As Price
        Public Property Genres As List(Of Genre)
        Public Property Images As List(Of Image)
        Public Property Position As Integer
        Public Property ChartOwner As BaseObject
        Public Property UpdatedDate As Date
        Public Property TrackCount As Integer

        Public Sub New()
            Me.Genres = New List(Of Genre)
            Me.Images = New List(Of Image)
        End Sub

        Public Shared Function FromBPObject(cha As BPOChart) As Chart
            If cha Is Nothing Then Return Nothing
            Dim newChart As New Chart()

            With newChart
                .ID = cha.id
                .Name = cha.name
                .Slug = cha.slug
                .Description = cha.description
                Date.TryParseExact(cha.publishDate, "yyyy-MM-dd", Nothing, Globalization.DateTimeStyles.None, .PublishDate)
                .Price = Price.FromBPObject(cha.price)

                For Each g As BPOGenreBase In cha.genres
                    .Genres.Add(New Genre() With {.ID = g.id, .Name = g.name, .Slug = g.slug})
                Next

                For Each i As KeyValuePair(Of String, BPOImage) In cha.images
                    .Images.Add(Image.FromBPObject(i))
                Next

                .Position = cha.position
                If cha.chartOwner IsNot Nothing Then
                    .ChartOwner = New BaseObject() With {.ID = cha.chartOwner.id, .Name = cha.chartOwner.name, .Slug = cha.chartOwner.slug}
                End If

                Date.TryParseExact(cha.updatedDate, "yyyy-MM-dd HH:mm:ss", Nothing, Globalization.DateTimeStyles.None, .PublishDate)
                .TrackCount = cha.trackCount
            End With

            Return newChart
        End Function
    End Class
End Namespace